package com.xsjiong.hoofen;

import android.content.Context;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import com.xsjiong.hoofen.entities.Student;

import java.util.List;

public class StudentAdapter extends BaseAdapter {
	private List<Student> D;

	public StudentAdapter(List<Student> data) {
		this.D = data;
	}

	@Override
	public int getCount() {
		return D.size();
	}

	@Override
	public Object getItem(int position) {
		return D.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Context cx = parent.getContext();
		Student stu = D.get(position);
		CardView view = new CardView(cx);
		int pd = UI.dp2px(10);
		view.setContentPadding(pd, pd, pd, pd);
		AppCompatTextView name = new AppCompatTextView(cx);
		name.setText(stu.name);
		view.setBackgroundColor(UI.ThemeColor);
		name.setTextColor(UI.AccentColor);
		LinearLayoutCompat d = new LinearLayoutCompat(cx);
		d.setOrientation(LinearLayoutCompat.HORIZONTAL);
		LinearLayoutCompat.LayoutParams para = new LinearLayoutCompat.LayoutParams(0, -2);
		para.weight = 1;
		d.addView(name, para);
		name = new AppCompatTextView(cx);
		name.setText(stu.totalRanks[0] + "名");
		name.setTextColor(UI.AccentColor);
		d.addView(name, -2, -2);
		LinearLayoutCompat con = new LinearLayoutCompat(cx);
		con.setOrientation(LinearLayoutCompat.VERTICAL);
		con.addView(d);
		// 下面部分
		d = new LinearLayoutCompat(cx);
		d.setOrientation(LinearLayoutCompat.HORIZONTAL);
		name = new AppCompatTextView(cx);
		name.setTextColor(UI.AccentColor);
		name.setText(stu.clsID + "班");
		d.addView(name, para);
		name = new AppCompatTextView(cx);
		name.setText(Float.toString(stu.scores[0]));
		name.setTextColor(UI.AccentColor);
		d.addView(name, -2, -2);
		con.addView(d, -1, -2);
		view.addView(con);
		con = new LinearLayoutCompat(cx);
		para = new LinearLayoutCompat.LayoutParams(-1, -1);
		pd = UI.dp2px(10);
		para.setMargins(pd, pd, pd, pd);
		con.addView(view, para);
		return con;
	}
}